using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
namespace DalatCooking.Core
{
    class CourseImageAccessor:ICourseImageRepository
    {
        public override bool Insert(CourseImage entity)
        {
            object rs = IDataProvider.Instance.OExecuteNonQuery("@ImageID","CourseImage_Insert",entity.ImageID,entity.CourseID,entity.ImageUrl,entity.Alt,entity.Title,entity.IsPublished);
            return int.Parse(rs.ToString()) > 0;
        }
        public override bool Update(CourseImage entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("CourseImage_Update", entity.ImageID, entity.CourseID, entity.ImageUrl, entity.Alt, entity.Title, entity.IsPublished);
            return rs > 0;
        }
        public override bool Delete(int id)
        {
            return IDataProvider.Instance.ExecuteNonQuery("CourseImage_Delete", id) > 0;
        }
        public override bool Delete(CourseImage entity)
        {
            return Delete(entity.ImageID);
        }
        public override List<CourseImage> FindAll()
        {
            return CBO.FillCollection<CourseImage>(IDataProvider.Instance.ExecuteReader("CourseImage_FindAll"));
        }
        public override CourseImage FindById(int id)
        {
            return CBO.FillObject<CourseImage>(IDataProvider.Instance.ExecuteReader("CourseImage_FindById", id));
        }
        public override CourseImage Single(int id)
        {
            return FindById(id);
        }
        public override CourseImage FindByName(string name)
        {
            throw new NotImplementedException();
        }
        public override List<CourseImage> PagingSql(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("CourseImage_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<CourseImage>(reader);
        }
        public override List<CourseImage> PagingLinq(int pageSize, int pageNum, out int totalRowCount)
        {
             var query = from c in FindAll()
                    select c;
                    totalRowCount = FindAll().Count;
            var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
            return list;
        }
        public override bool SetActive(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("CourseImage_SetActive", id);
            return rs > 0;
        }
        public override bool SetSortIndex(int id, int isup)
        {
            return IDataProvider.Instance.ExecuteNonQuery("CourseImage_SetSortIndex", id, isup) > 0;
        }
        public override bool RemoveSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("CourseImage_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RemoveAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("CourseImage_RemoveAll");
            return rs > 0;
        }
        public override bool RestoreSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("CourseImage_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RestoreAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("CourseImage_RestoreAll");
            return rs > 0;
        }
    }
}